package action.cart;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import option.OperationEnum;

import service.BookService;
import util.CookieUtil;
import entity.Book;
import entity.Cart;
import entity.CartItem;

/**
 * 购物车相关操作
 */
@SuppressWarnings("serial")
public class CartServlet extends HttpServlet{
//	private static Logger log = Logger.getLogger(CartServlet.class);
	
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = (String) request.getAttribute("action");
		if (action.equals(OperationEnum.CART_LIST.getValue())) {//跳转到购物车列表页面
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			if (cart == null) {
				cart = new Cart();
				String cartInfo = CookieUtil.findCookie("cart", request);
				cart.stringToCart(cartInfo);
				request.getSession().setAttribute("cart", cart);
			}
			
			request.getRequestDispatcher("/WEB-INF/pages/cart/cart_list.jsp").forward(request, response);
		} else if (action.equals(OperationEnum.CART_BUY.getValue())) {//购买图书，添加到购物车
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			if (cart == null) {
				cart = new Cart();
				String cartInfo = CookieUtil.findCookie("cart", request);
				cart.stringToCart(cartInfo);
			}
			
			Integer id = Integer.parseInt(request.getParameter("id"));
			BookService service = new BookService();
			Book book = null;
			try {
				book = service.queryBookById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			CartItem ci = new CartItem();
			ci.setProduct(book);
			ci.setCount(1);
			boolean result = cart.add(ci);
			
			request.getSession().setAttribute("cart", cart);
			CookieUtil.addCookie("cart", cart.cartToString(), response);
			PrintWriter out = response.getWriter();
			out.write(String.valueOf(result));
			out.close();
		} else if (action.equals(OperationEnum.CART_UPDATE.getValue())) {//更新购物车中具体商品的数量
			Integer id = Integer.parseInt(request.getParameter("id"));
			Integer count = Integer.parseInt(request.getParameter("count"));
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			cart.modify(id, count);
			
			request.getSession().setAttribute("cart", cart);
			CookieUtil.addCookie("cart", cart.cartToString(), response);
			response.sendRedirect(request.getContextPath() + "/cart/cartList.do");
		} else if (action.equals(OperationEnum.CART_DELETE.getValue())) {//删除购物车中的某件商品
			Integer id = Integer.parseInt(request.getParameter("id"));
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			cart.delete(id);
			
			request.getSession().setAttribute("cart", cart);
			CookieUtil.addCookie("cart", cart.cartToString(), response);
			response.sendRedirect(request.getContextPath() + "/cart/cartList.do");
		} else if (action.equals(OperationEnum.CART_REBUY.getValue())) {//重新购买删除的某件商品
			Integer id = Integer.parseInt(request.getParameter("id"));
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			cart.rebuy(id);
			
			request.getSession().setAttribute("cart", cart);
			CookieUtil.addCookie("cart", cart.cartToString(), response);
			response.sendRedirect(request.getContextPath() + "/cart/cartList.do");
		} else if (action.equals(OperationEnum.CART_CLEAR_DELETED.getValue())) {
			Cart cart = (Cart) request.getSession().getAttribute("cart");
			cart.clearDeleted();
			
			request.getSession().setAttribute("cart", cart);
			CookieUtil.addCookie("cart", cart.cartToString(), response);
			response.sendRedirect(request.getContextPath() + "/cart/cartList.do");
		}
	}
}
